import redis
import requests
from wxpy import Bot
from apscheduler.schedulers.blocking import BlockingScheduler
from datetime import date, timedelta, datetime
import time

conn = redis.Redis(db=3)
conn_0 = redis.Redis(db=0)
bot = Bot(console_qr=2, cache_path="botoo.pkl")
# grp = bot.friends().search('8583')[0]

prov_cunm = {'上海': 3,
             '云南': 2,
             # '内蒙': 5,
             '北京': 2,
             '吉林': 2,
             '四川': 3,
             '天津': 3,
             '宁夏': 2,
             '安徽': 2,
             '山东': 2,
             '山西': 2,
             '广东': 3,
             '广西': 2,
             '新疆': 2,
             '江苏': 2,
             '江西': 3,
             '河北': 2,
             '河南': 2,
             '浙江': 2,
             '海南': 2,
             '湖北': 2,
             '湖南': 2,
             '甘肃': 2,
             '福建': 2,
             '西藏': 2,
             '贵州': 2,
             '辽宁': 2,
             '重庆': 3,
             '陕西': 2,
             '青海': 2,
             '黑龙江': 2,
             '内蒙古': 2}


# def job():
#     yesterday = date.today() - timedelta(1)
#     print(yesterday)
#     yesterday = yesterday.strftime('%Y%m%d')
#     res = conn.hgetall('8081_' + yesterday)
#     res = {k.decode(): v.decode() for k, v in res.items()}
#     message = """日期%s
# =========8081=========
# 总额：%s，笔数：%s""" % (yesterday, res.get('amount'), res.get('count'))
#     grp.send(message)
#     res = conn.hgetall('8091_' + yesterday)
#     res = {k.decode(): v.decode() for k, v in res.items()}
#     message = """日期%s
# =========8091=========
# 移动：%s，笔数：%s
# 联通：%s，笔数：%s""" % (
#         yesterday, res.get('cmcc'), res.get('cmcc_count'), res.get("unicom"), res.get("unicom_count"))
#     grp.send(message)


def job2():
    conn_0.set("close_unicom_no", 100)


def job3():
    print(datetime.now())
    un_amount = conn_0.hget('unicom_unkonw', '成功金额')
    if not un_amount:
        time.sleep(1)
        un_amount = conn_0.hget('unicom_unkonw', '成功金额')
    cm_amount = conn_0.hget('cmcc_amount', '成功金额')
    un_amount = int(un_amount.decode())

    if not cm_amount:
        time.sleep(1)
        cm_amount = conn_0.hget('cmcc_amount', '成功金额')

    if cm_amount:
        cm_amount = int(cm_amount.decode())
    else:
        cm_amount = 0

    ct_amount = conn_0.hget('ctcc_amount', '成功金额')
    if ct_amount:
        ct_amount = int(ct_amount.decode())
    else:
        ct_amount = 0

    g_100 = bot.groups().search('100')[0]
    g_100.send("移动：%d， 联通：%d,电信：%d 总额：%d" % (cm_amount, un_amount, ct_amount, ct_amount + un_amount + cm_amount))


def job4():
    conn_0.set("pre_un_queue_len_M2018081416281534235296_30", 10)
    conn_0.set("pre_un_queue_len_M2018081416281534235296_50", 10)
    conn_0.set("pre_un_queue_len_M2018081416281534235296_100", 10)
    conn_0.set("is_cmcc86_close", 0)


def job5():
    conn_0.set("pre_un_queue_len_M2018081416281534235296_30", 30)
    conn_0.set("pre_un_queue_len_M2018081416281534235296_50", 30)
    conn_0.set("pre_un_queue_len_M2018081416281534235296_100", 30)


def job6():
    conn_0.hmset('un_prov_cnum', prov_cunm)


def job7():
    #url = 'http://admin.91bill.cn/data/JSON_DAY_BIZ_TOTAL_1.php'
    #rsp = requests.get(url)
    #text = rsp.text
    g_100 = bot.groups().search('core')[0]
    #g_100.send(text)

    res = conn_0.get("all_recharge")
    # print(res)
    g_100.send(res.decode())

def job8():
    # 关闭
    conn_0.set("is_cmcc86_close", 1)


def job9():
    # 开启
    conn_0.set("is_cmcc86_close", 0)


def job10():
    print("检测省份")
    for prov in prov_cunm:
        url = 'http://127.0.0.1:10010/get/cmcc_status/%s' % prov
        rsp = requests.get(url)
        res = rsp.json()
        print(res, prov)
        if res['code'] != '0':
            conn_0.sadd("unused_86_prov", prov)
            g_100 = bot.groups().search('127星恒太')[0]
            g_100.send("86%s维护" % prov)

def job11():
    message = conn_0.smembers('unused_un_prov')
    text =  ";".join(i.decode() for i in message)
    text = "联通关闭省份：" + text
    g_100 = bot.groups().search('127星恒太')[0]
    g_100.send(text)

# BlockingScheduler
scheduler = BlockingScheduler()
# scheduler.add_job(job, 'cron', hour=0, minute=30)
scheduler.add_job(job2, 'cron', hour=0, minute=1)
scheduler.add_job(job3, 'cron', hour="7-23", minute=59)
scheduler.add_job(job8, 'cron', hour="22-23", minute=0)
scheduler.add_job(job9, 'cron', hour="22-23", minute=10)
# scheduler.add_job(job10, 'cron', hour="22-23", minute=8)
#scheduler.add_job(job4, 'cron', hour=2, minute=0)
# scheduler.add_job(job5, 'cron', hour=16, minute=59)
scheduler.add_job(job6, 'cron', hour=0, minute=1)
scheduler.add_job(job7, 'cron', hour="7-23", minute=59)
scheduler.add_job(job11, 'cron', hour="0-17", minute="*/30")
scheduler.add_job(job11, 'cron', hour="17-23", minute="*/5")



scheduler.start()
